﻿using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace BindTryParseMVC.Models
{
    public class Contact
    {
        public Guid Id { get; set; }

        [ValidateName(ErrorMessage ="Name must not contain `zz`")]
        public string? Name { get; set; }

        public string? Email { get; set; }

        public string? PhoneNumber { get; set; }
    }

    public class ErrorViewModelContact
    {
        public string? RequestId { get; set; }

        public bool ShowRequestId=>!string.IsNullOrEmpty(RequestId);

        public string Message { get; set; }= string.Empty;
    }

    public class ValidateNameAttribute : ValidationAttribute
    {
        public ValidateNameAttribute()
        {
            const string defaultErrorMessage = "Error with Name";
            ErrorMessage ??= defaultErrorMessage;
        }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if(value ==null|| string.IsNullOrWhiteSpace(value.ToString()))
            {
                return new ValidationResult("Name is required.");
            }

            if (value.ToString()!.ToLower().Contains("zz"))
            {
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
            return ValidationResult.Success;
        }
    }


    public class ValidationResultErrorMessageContext:DbContext
    {
        public ValidationResultErrorMessageContext(DbContextOptions<ValidationResultErrorMessageContext> options):base(options) 
        {
            
        }

        public DbSet<Contact> Contact { get; set; } = default!;
    }
}
